package com.demo.jinjunhuang.viewsdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author jianjunhuang.me@foxmail.com
 *         create on 2017/8/4.
 */

public class TestView extends View {

    private Paint paint;

    private Path path;

    public TestView(Context context) {
        super(context);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            initPaint();
        }
    }

    public TestView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            initPaint();
        }
    }

    public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            initPaint();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPaint();
    }

    /*
    MeasureSpec : 通过它来帮助我们测量 View。MeasureSpec 是一个 32位的 int值，其中高 2 位为测量的模式，低 30 位为测量的大小，在计算中使用位运算来提高并优化效率
    测量模式：
    - EXACTLY
      精确模式，即指定大小或 match_parent
    - AT_MOST
      最大值模式，当控件的属性为 wrap_content 时，控件大小一般随着控件的子控件或内容的变化而变化，此时控件的尺寸只要不超过父控件允许的最大尺寸即可
    - UNSPECIFIED
      不指定其大小测量模式，通常绘制自定义 View 时才会用
      父布局没有限定子布局大小，它可以是任何大小。

    View 默认 onMeasure 方法
    */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureSpec(widthMeasureSpec), measureSpec(heightMeasureSpec));
    }

    private int measureSpec(int spec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(spec);
        int specSize = MeasureSpec.getSize(spec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else if (specMode == MeasureSpec.AT_MOST) {
            result = 200;
        } else if (specMode == MeasureSpec.UNSPECIFIED) {
            result = 200;
        }
        return result;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initPaint() {
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
//        paint.setStrokeWidth(10);

        //线性渐变
        LinearGradient linearGradient = new LinearGradient(100, 150, 250, 150, ContextCompat.getColor(getContext(), R.color.colorAccent), ContextCompat.getColor(getContext(), R.color.colorPrimary), Shader.TileMode.REPEAT);
        //辐射渐变
        RadialGradient radialGradient = new RadialGradient(300, 200, 200, ContextCompat.getColor(getContext(), R.color.colorAccent), ContextCompat.getColor(getContext(), R.color.colorPrimary), Shader.TileMode.CLAMP);
        //扫描渐变
        SweepGradient sweepGradient = new SweepGradient(300, 200, ContextCompat.getColor(getContext(), R.color.colorAccent), ContextCompat.getColor(getContext(), R.color.colorPrimary));
        paint.setShader(sweepGradient);
        /*
        设置点的形状
        ROUND - 圆头
        BUTT - 平头
        SQUARE - 方头
        */
//        paint.setStrokeCap(Paint.Cap.SQUARE);
        //抗锯齿
        paint.setAntiAlias(true);


        path = new Path();
        /*
        添加一个圆
        最后一个是画圆的路径方向
        CW - Clockwise 顺时针
        CCW - counter-clockwise 逆时针
         */
//        path.addCircle(300, 300, 50, Path.Direction.CW);

        //N
//        path.moveTo(100, 100);
//        path.lineTo(100, 300);
//        path.lineTo(300, 100);
//        path.lineTo(300, 300);

//        path.moveTo(100, 100);
//        path.lineTo(200, 100);
//        path.moveTo(100, 100);
//        //最后一个 表示 是否显示隐藏的轨迹
//        path.arcTo(0, 200, 200, 300, -90, 180, false);
//        //封闭子图
//        path.close();


    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int radius = 200;
//        canvas.drawCircle(radius * 2, radius * 2, 200, paint);
//        canvas.drawPoint(radius, radius, paint);
        /*
        开始位置默认在椭圆右轴，正数顺时针转，负数逆时针转
        角度同上，正数顺时针转，负数逆时针转
        布尔值 表示是否连接圆心 ， true 为扇形， false 则为弧
         */
//        canvas.drawArc(50, 50, 100, 100, 90, 60, false, paint);

//        canvas.drawPath(path, paint);
        canvas.drawRoundRect(100, 100, 500, 300, 10, 100, paint);

    }
}
